package com.cse333.jester;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.cse333.jester1.R;



public class CurrentSongPanel extends SurfaceView implements SurfaceHolder.Callback{
	private CanvasThread canvasthread;
	
	int backgroundInt = R.drawable.cubes;
	
    public CurrentSongPanel(Context context, AttributeSet attrs) {
		super(context, attrs); 
		// TODO Auto-generated constructor stub
	    getHolder().addCallback(this);
	    canvasthread = new CanvasThread(getHolder(), this);
	    setFocusable(true);
	}

	 


	 public CurrentSongPanel(Context context) {
		   super(context);
		    getHolder().addCallback(this);
		    canvasthread = new CanvasThread(getHolder(), this);
		    setFocusable(true);

	    }


	@Override
	public void onDraw(Canvas canvas) {
		Log.d("ondraw", "lefutott");
		Paint paint = new Paint();
		int isRusty = 1;
		
		try{
		drawNowPlayingScreen(canvas);}
		catch (NullPointerException npe){
			
		}
		
		
	}
	 
	 
	 
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		// TODO Auto-generated method stub
	    canvasthread.setRunning(true);
	    canvasthread.start();

		
	}
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		// TODO Auto-generated method stub
		boolean retry = true;
		canvasthread.setRunning(false);
		while (retry) {
			try {
				canvasthread.join();
				retry = false;
			} catch (InterruptedException e) {
				// we will try it again and again...
			}
		}

	}
	
	
	
	public void drawNowPlayingScreen(Canvas canvas){
		Bitmap albumArt = CurrentTrack.getAlbumArt();
		Bitmap background = BitmapFactory.decodeResource(getResources(), backgroundInt);
		canvas.drawColor(Color.BLACK);
		
		
		
		
        int screenWidthDynamic = getWidth();
        double albumScale = ((double)(screenWidthDynamic-100))/albumArt.getWidth();
        double backgroundScale = ((double)(screenWidthDynamic))/background.getWidth();
        
        
        int scaledAlbumWidth = (int)(albumArt.getWidth()*albumScale);
        int scaledAlbumHeight = (int)(albumArt.getHeight()*albumScale);
        
        int scaledBackgroundWidth = (int)(background.getWidth()*backgroundScale);
        int scaledBackgroundHeight = (int)(background.getHeight()*backgroundScale);
        
        Bitmap scaledBackground = Bitmap.createScaledBitmap( background, scaledBackgroundWidth, scaledBackgroundHeight, true );
        Bitmap scaledAlbumArt = Bitmap.createScaledBitmap( albumArt, scaledAlbumWidth, scaledAlbumHeight, true );
        
		canvas.drawBitmap(scaledBackground, 0, 0, null);	
		
		Typeface tf = Typeface.create("Roboto", Typeface.DEFAULT.getStyle());
		
		Paint paint = new Paint();
		paint.setTypeface(tf);
		paint.setTextSize(100);
		paint.setColor(Color.WHITE);
		
		canvas.drawText(CurrentTrack.getTitle(),50,190,paint);
		paint.setTextSize(90);
		canvas.drawText(CurrentTrack.getArtist(),50,910,paint);
		Typeface tf2 = Typeface.create("Roboto Light", Typeface.DEFAULT.getStyle());
		paint.setTypeface(tf2);
		paint.setTextSize(80);
		canvas.drawText(CurrentTrack.getAlbum(),50,1000,paint);
		
		
		canvas.drawBitmap(scaledAlbumArt, 50, 220, null);
		
	}
	
	


}   